﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Threading;

using ProtoImaginer.Models;
using ProtoImaginer.ProtoControl;
namespace ProtoImaginer.ProtoDemo
{
    public partial class ProjectDemo : PhoneApplicationPage
    {

        private Models.Page page;
        private Models.Image image;
        private List<Models.Control> controls;
        private List<ControlAtom> controlItems;

        public ProjectDemo()
        {
            InitializeComponent();
            updatePage(new Models.Page("-1"));
            TitleFadeOut();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            PageTitle.Text = NavigationContext.QueryString["PageIdToSend"];
        } 

        #region UI handler

        private void CanvasPinchStarted(object sender, PinchStartedGestureEventArgs e){}

        private void CanvasPinchDelta(object sender, PinchGestureEventArgs e)
        {
            if (e.DistanceRatio < 1)
            {
                var transform = (CompositeTransform)DemoCanvas.RenderTransform;
                transform.ScaleX *= e.DistanceRatio;
                transform.ScaleY = transform.ScaleX;
            }
        }

        private void CanvasPinchCompleted(object sender, PinchGestureEventArgs e)
        {
            var transform = (CompositeTransform)DemoCanvas.RenderTransform;
            if(transform.ScaleX < 0.8)
                NavigationService.GoBack();
            else
            {
                transform.ScaleX = 1;
                transform.ScaleY = 1;
            }
        }

        private void CanvasTap(object sender, GestureEventArgs e)
        {
            if (!e.Handled) { 
                //TODO flash all controls for times

                //alt
                foreach (ControlAtom control in controlItems)
                    control.ControlRect.Opacity = 1;
            }
        }

        private void ControlAtomTap(object sender, GestureEventArgs e)
        {
            e.Handled = true;
            //TODO UI animation jump to next page

            //TODO get page according to control
            updatePage(new Models.Page("-1"));
        }

        #endregion

        #region UI misc

        private void TitleFadeOut()
        {
            //TODO fadeout animation needed

            //alt
            TitlePanel.Visibility = System.Windows.Visibility.Collapsed;
        }

        private void BindControlItemListener(ControlAtom newItem)
        {
            GestureListener listener = GestureService.GetGestureListener(newItem);
            listener.Tap += ControlAtomTap;
        }

        #endregion

        #region data related operation

        private void updatePage(Models.Page newPage){

                //TODO get imageId according to PageId

                //alt
                Models.Image image = new Models.Image();

                UpdateImage(image);
        }

        private void UpdateImage(Models.Image newImage) {

                //update UI
                BitmapImage img = new BitmapImage();
                img.SetSource(Application.GetResourceStream(new Uri(@"/ProtoImaginer;component/Images/afterprocess.image.PNG", UriKind.Relative)).Stream);
                Image.Source = img;

                //TODO get ControlList according to imageId
                List<Models.Control> controls = new List<Models.Control>();

                UpdateControls(controls);
        }

        private void UpdateControls(List<Models.Control> newControls) {

            foreach (ControlAtom controlItem in controlItems) {
                ControlContainer.Children.Remove(controlItem);
            }
            controlItems = null;
            foreach (Models.Control control in newControls)
                AddControl(control);
        }

        //add control
        private void AddControl(Models.Control control)
        {
            //initialize new control item.
            ControlAtom newItem = new ControlAtom(control);

            try
            {
                //database related
                controlItems.Add(newItem);

                //UI related
                ControlContainer.Children.Add(newItem);

                //bind listeners
                BindControlItemListener(newItem);

                Canvas.SetLeft(newItem, control.Control_Top_Left_Point_X_Coordinate);
                Canvas.SetTop(newItem, control.Control_Top_Left_Point_Y_Coordinate);
            }
            catch (Exception e)
            { 
                MessageBox.Show("Error: " + e.Message);
            }

        }
        #endregion
    }
}